package com.bailong.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    /**
     * 请求映射
     * http://localhost:8080/hello --->http://localhost:8080/web-inf/jsp/hello.jsp
     * @return
     */
    @RequestMapping("hello")
    public String hello(){
        System.out.println("跳转到hellourl");
        return "hello";
    }

    /**
     * 此方法用于跳转到login.jsp，因为在web-inf当中的资源是无法直接访问的，通过spring-mvc的配置驱动才可访问
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 此方法用于处理登录的逻辑
     * @return
     */
    @RequestMapping("login")
    private String login(String username,String password){
        System.out.println("账号："  + username);
        System.out.println("密码："  + password);
        return "hello";
    }

    @RequestMapping("login2")
    private String login2(User user, HttpServletRequest request){
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            request.setAttribute("msg",":登录成功，欢迎" + user.getUsername());
            return "hello";
        }else {
            request.setAttribute("msg","账号密码错误");
            return "login2";
        }
    }
    @RequestMapping("login3")
    private String login3(User user, Map<String,Object> map){
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            map.put("msg",":登录成功，欢迎" + user.getUsername());
            return "hello";
        }else {
            map.put("msg","账号密码错误");
            return "login";
        }
    }
    @RequestMapping("login4")
    private String login4(User user, Model model){
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            model.addAttribute("msg",":登录成功，欢迎" + user.getUsername());
            return "hello";
        }else {
            model.addAttribute("msg","账号密码错误");
            return "login";
        }
    }
    @RequestMapping("login5")
    private void login5(User user, HttpServletResponse httpServletResponse, PrintWriter printWriter) throws IOException {
        httpServletResponse.setContentType("text/html;charset=utf-8");
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            httpServletResponse.getWriter().println("登录成功！！！");
        }else {
            httpServletResponse.getWriter().println("登录失败！！！");
        }
    }
    @ResponseBody
    @RequestMapping("login6")
    private Object login6(User user,HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.setContentType("text/html;charset=utf-8");
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            List<User>users = new ArrayList<User>();
            users.add(user);
            User user1 = new User();
            user1.setPassword("23456");
            user1.setUsername("dawew");
            users.add(user1);
          return users;
        }else {
           return "登录失败！！！";
        }
    }

    @RequestMapping("login7")
    private String login7(User user,Model model) throws IOException {
        System.out.println("账号："  + user.getUsername());
        if("zhangsan".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            model.addAttribute("msg","登录成功！！");
            //实现请求转发
//            return "forward:hello";
            //重定向
            return "redirect:hello";
        }else {
            model.addAttribute("msg","登录失败！！");
            return "login";
        }

    }
}
